package com.cci.kangdao.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import com.cci.kangdao.dao.model.ServiceAgreement;

import java.util.List;
import java.util.Map;

@MapperScan
public interface ServiceAgrementDao {

	@Select("select ServicePhone from User_T where ID = #{userId}")
	public String getServicePhone(Long userId);

	@Insert("INSERT INTO ServiceAgreement_T (UserId ,SignTime ,TerminateTime ,Creator ,CreateTime, Type ,ServiceStationId,version)\r\n" +
			"     VALUES (#{userId}, #{signTime}, #{terminateTime}, #{creator}, #{createTime}, #{type} , #{serviceStationId},#{version})")
	public int signServiceAgreement(ServiceAgreement serviceAgreement);

	//除了经销商，其他用户判断是否签没签过协议走这个sql
	@Select("select ID, UserId, SignTime, TerminateTime, Creator, CreateTime, Deleter, DeleteTime,version from ServiceAgreement_T where UserId = #{userId} and Type = #{type} and Flag = 0")
	public ServiceAgreement getServiceAggrementDetail(@Param("userId") Long userId, @Param("type") int type);



	@Select("<script> select count(1) as count from ServiceAgreement_T where UserId = #{userId} and Type = #{type} and Flag = 0 and UserId in  <foreach collection='userIdList' item='item' open='(' separator=',' close=')'>#{item} </foreach>  </script>"  )
	 int getServiceAggrementList(@Param("userIdList") List<Long> userIdList, @Param("type") int type);
	//查询是否签约
	@Select("select count(1) as count from ServiceAgreement_T where UserId = #{userId} and  TerminateTime >= #{time} and Flag = 0 and type=0")
	public Long isSignAgreement(@Param("userId") Long userId, @Param("time") String time);

	//经销商判断是否签没签过协议走这个sql
	@Select("select ID, UserId, SignTime, TerminateTime, Creator, CreateTime, Deleter, DeleteTime from ServiceAgreement_T where Type = #{type} and Flag = 0 and ServiceStationId = #{serviceStationId}")
	public ServiceAgreement getServiceAggrementDetailByZZ(@Param("type") int type, @Param("serviceStationId") Long serviceStationId);

	@Update("UPDATE ServiceAgreement_T SET Flag = -1,Deleter = #{deleter}, DeleteTime= #{deleteTime} WHERE ID = #{id}")
	public int terminateServiceAgreement(ServiceAgreement serviceAgreement);

	@Update("update ServiceAgreement_T set Flag = -1 where TerminateTime < #{currentDate}")
	public int updateServiceAgreementStatus(String currentDate);


	List<Map<String, Object>> getLicensePlateNumberByEsn(@Param("esnList") List<String> esnList);


//	@Select("select count(1) as count from ServiceAgreement_T where UserId = #{userId} and  TerminateTime >= #{time} and Flag = 0 and type = #{type} and version=#{version}")
//	public Long isSignAgreementByType(@Param("userId") Long userId,@Param("time") String time,@Param("type") Integer type,@Param("version") Integer version);

	@Select("select ID, UserId, SignTime, TerminateTime, Creator, CreateTime, Deleter, DeleteTime,version from ServiceAgreement_T where UserId = #{userId} and Type = #{type} and Flag = 0  and version=#{version} and TerminateTime >= #{time}")
	public ServiceAgreement getServiceAggrementDetailAndVersion(@Param("userId") Long userId, @Param("time") String time, @Param("type") int type, @Param("version") Integer version);

	@Select("select ID, UserId, SignTime, TerminateTime, Creator, CreateTime, Deleter, DeleteTime,type from ServiceAgreement_T where ID = #{id} ")
	ServiceAgreement selectById(@Param("id") Long id);
}
